﻿// Requires ScriptManager

var pe_loadingContent = '<img src="/umbraco/images/tree/wait.gif" />';
var pe_container = "currPermissions";
var pe_selectedNodes = new Array();
var pe_checkedPermissions = new Array();

//Event Handlers:
function nodePermissions_ItemChecked(items)
{    
    //replace the commas with nothing
    pe_checkedPermissions = items.replace(/,/g,"");
}
function objTree_NodeChecked(nodes)
{
    pe_selectedNodes = nodes.split(",");
    $get('checkedNodes').innerHTML = nodes;
}
//End Event Handlers

function BeginShowNodePermissions(selectedID, userID)
{
    SetUpdateProgress();
    setTimeout("ShowNodePermissions(" + selectedID + "," + selectedID + "," + userID + ");", 10);
}
function ShowNodePermissions(nodeID, selectedID, userID)
{   
    umbraco.cms.presentation.user.PermissionsHandler.GetNodePermissions(parseInt(userID), parseInt(nodeID), ShowNodePermissionsCallback);
}
function ShowNodePermissionsCallback(result) 
{
    $get(pe_container).innerHTML = result;
}
function BeginSavePermissions()
{
    //ensure that there are nodes selected to save permissions against
    if (pe_selectedNodes.length == 0)
    {
        alert("No nodes have been selected");
        return false;
    }
    else
    {
        if (!confirm("Permissions will be changed for nodes: " + pe_selectedNodes.join(",") + ". Are you sure?"))
            return false;
    }
    
    var replaceChildren = $get("chkChildPermissions").checked;    
    var userID = $get("hidUserID").value;
   
    SetUpdateProgress();
    
    setTimeout("SavePermissions('" + pe_selectedNodes.join(",") + "'," + userID + ",'" + pe_checkedPermissions + "'," + replaceChildren + ");", 10);
    return false;
}
function SavePermissions(nodeIDs, userID, selectedPermissions, replaceChildren)
{    
    umbraco.cms.presentation.user.PermissionsHandler.SaveNodePermissions(parseInt(userID), nodeIDs.split(","), selectedPermissions, replaceChildren, SavePermissionsHandler);    
}
function SavePermissionsHandler(result)
{
    if (top!=null && top.UmbSpeechBubble!=null)
        top.UmbSpeechBubble.ShowMessage("save","Saved","Permissions Saved");        
    $get(pe_container).innerHTML = result;
}

//shows the ajax clock/waiting thing
function SetUpdateProgress()
{
    $get(pe_container).innerHTML = pe_loadingContent;
}